package level.editor.view.base;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import level.editor.control.main.MainTabbed;
import level.editor.view.Window;
import level.editor.view.desktop.Desktop;
import level.editor.view.group.Group;

public class BasePanel extends JPanel
{
	public static final Integer Z_ORDER = new Integer(Integer.MIN_VALUE);

	public BasePanel(Desktop desktop)
	{
		super(new BorderLayout());
		init(desktop);
	}

	public void updateBasePanel(Dimension dimension)
	{
		setSize(dimension);
	}

	public void addPanel(Group component, Boolean direction)
	{
		if ((leftPanel == null && direction.equals(PanelConstants.LEFT))
				|| (rightPanel == null && direction
						.equals(PanelConstants.RIGHT)))
		{
			JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
			split.setBorder(null);
			split.setDividerLocation(direction.equals(PanelConstants.LEFT) ? component
					.getSize().width + 10 : getSize().width
					- component.getSize().width - 15);
			split.setResizeWeight(direction.equals(PanelConstants.LEFT) ? 0.0
					: 1.0);
			split.setLeftComponent(direction.equals(PanelConstants.LEFT) ? component
					: getPanel());
			split.setRightComponent(direction.equals(PanelConstants.RIGHT) ? component
					: getPanel());
			leftPanel = (direction.equals(PanelConstants.LEFT) ? component
					: leftPanel);
			rightPanel = (direction.equals(PanelConstants.RIGHT) ? component
					: rightPanel);
			removeAll();
			add(split, BorderLayout.CENTER);
			component.removeCheck();
			revalidate();
		}
		else
			System.out.println("Panel has already been added");
		Runtime.getRuntime().gc();
	}

	public MainTabbed getMainTabbed()
	{
		return centerPanel.getMainTabbed();
	}

	public void removePanel(Boolean direction)
	{
		Boolean redraw = false;
		if (leftPanel != null && direction.equals(PanelConstants.LEFT))
		{
			leftPanel = null;
			redraw = true;
		}
		else if (rightPanel != null && direction.equals(PanelConstants.RIGHT))
		{
			rightPanel = null;
			redraw = true;
		}

		if (redraw)
		{
			if (direction.equals(PanelConstants.LEFT))
			{
				JSplitPane split = (JSplitPane) getPanel();
				if (!(split.getLeftComponent() instanceof JSplitPane))
				{
					Component comp = split.getRightComponent();
					removeAll();
					add(comp, BorderLayout.CENTER);
				}
				else
				{
					int div = split.getDividerLocation();
					split.setLeftComponent(centerPanel);
					split.setDividerLocation(div);
				}
			}
			else if (direction.equals(PanelConstants.RIGHT))
			{
				JSplitPane split = (JSplitPane) getPanel();
				if (!(split.getRightComponent() instanceof JSplitPane))
				{
					Component comp = split.getLeftComponent();
					removeAll();
					add(comp, BorderLayout.CENTER);
				}
				else
				{
					int div = split.getDividerLocation();
					split.setRightComponent(centerPanel);
					split.setDividerLocation(div);
				}
			}
			revalidate();
		}
		else
			System.out.println("No panel to remove");
		Runtime.getRuntime().gc();
	}

	public void removePanel(Component component)
	{
		if (leftPanel == component)
		{
			removePanel(PanelConstants.LEFT);
		}
		else if (rightPanel == component)
		{
			removePanel(PanelConstants.RIGHT);
		}
	}

	public Desktop getDesktop()
	{
		return (Desktop) getParent();
	}

	public Group getLeft()
	{
		return leftPanel;
	}

	public Group getRight()
	{
		return rightPanel;
	}

	public Boolean hasLeft()
	{
		if (leftPanel == null)
			return false;
		else
			return true;
	}

	public Boolean hasRight()
	{
		if (rightPanel == null)
			return false;
		else
			return true;
	}
	
	public void initDefaultHandler(Window window)
	{
		centerPanel.initDefaultHandler(window);
	}

	private void init(Desktop desktop)
	{
		setSize(desktop.getSize());
		setVisible(true);
		add(centerPanel = new CenterPanel(desktop), BorderLayout.CENTER);
		leftPanel = null;
		rightPanel = null;
	}

	private Component getPanel()
	{
		return (((BorderLayout) this.getLayout())
				.getLayoutComponent(BorderLayout.CENTER));
	}

	private CenterPanel centerPanel;
	private Group leftPanel;
	private Group rightPanel;
}
